package com.masiyi;

import com.masiyi.dao.UserDao;
import com.masiyi.entity.Configuration;
import com.masiyi.entity.User;
import com.masiyi.executor.SimpleSqlSession;
import com.masiyi.io.Resources;
import com.masiyi.parse.ConfigParse;
import com.masiyi.util.MyMybatisUtil;

import java.io.InputStream;
import java.util.List;

/**
 * @Author masiyi
 * @Date 2023/11/10
 * @PackageName:com.masiyi
 * @ClassName: MybatisTest
 * @Description: TODO
 * @Version 1.0
 */
public class MybatisTest {

    public static void main(String[] args) {

        //加载配置文件
        InputStream resourceAsSteam = Resources.getResourceAsStream("myMybatisConfig.xml");

        //解析Configuration对象
        Configuration configuration = new ConfigParse().parse(resourceAsSteam);

        //创建一个sqlSession,用于连接数据库
        SimpleSqlSession simpleSqlSession = new SimpleSqlSession(configuration);

        //创建代理类
        UserDao userDao = simpleSqlSession.newProxyClass(UserDao.class);

        //findById
        User user = new User();
        user.setId(1);
        userDao.findById(user).forEach(System.out::println);

        System.out.println("===================");

        //findAll
        userDao.findAll().forEach(System.out::println);

        // 释放资源
        simpleSqlSession.close();
    }


}
